#include<intersection_remesh_and_cut.h>

#define quote lib_cell_quote

static inline void merge_two_vertices(Mesh* m,template_v* v,template_v* v1)
{
    
    template_v* vs[2]={v,NULL};
    template_f* f=(template_f*)(v1->faces->value);
    if(f->vertices[0]==v1)
    {
        vs[1]=f->vertices[1];
    } 
    else
    {
        vs[1]=f->vertices[0];
    }

    m->delete_face(m,*f,true);
    m->create_facev(m,vs,2); 

}

static inline Node* adjust_edges_to_loop_from_nodes(Node* node,Mesh*m)
{
    if(node==NULL)
    {
        return NULL;
    }
    Node* n1=NULL,*n2=NULL;
    Node* nit=(Node*)(node->Next);n1=node;
    template_v* v=(template_v*)(n1->value),*v1=NULL;
    for(;nit!=NULL;nit=(Node*)(nit->Next))
    {
        v1=(template_v*)(nit->value);
        if(fabs(v->point[0]-v1->point[0])<1e-5&&fabs(v->point[1]-v1->point[1])<1e-5&&fabs(v->point[2]-v1->point[2])<1e-5)
        {
            n2=nit;
           // printf("find one \n");
            break;
        }
    }
    if(n1!=NULL&&n2!=NULL)
    {
       merge_two_vertices(m,v,v1);
    }




    Node* re=node_remove(node,n1);
    re=node_remove(re,n2);
    return re;
}

void adjust_mesh_topology(Mesh* m)
{

    Node* node1=NULL,*node4=NULL;
    printf("ajus\n");
    for(auto it=m->v_begin(m);it!=m->v_end(m);it++)
    {
        int size=node_size(quote(it)->faces);
        if(size==1)
        {
            Crossover_Point* cp=(Crossover_Point*)(quote(it)->prop);
            int flag=0;
            for(int i=0;i<2;i++)
            {
                if(cp->vs[i]!=NULL)
                {

                    if(m->vertex_is_boundary(m,*(cp->vs[i])))
                    {
                        flag=1;
                    }
                }
                else if(cp->fs[i]!=NULL)
                {
                    if(m->face_is_boundary(m,*(cp->fs[i])))
                    {
                        flag=1;
                    }
                }
            } 
            if(flag==0)
            {
                printf("jiance 1 %d %.8lf %.8lf %.8lf\n",quote(it)->id,quote(it)->point[0],quote(it)->point[1],quote(it)->point[2]);
                node1=node_overlying(node1,quote(it));
            }
        }
        else if(size==4)
        {
            printf("jiance 4:%d\n",quote(it)->id );
            node4=node_overlying(node4,quote(it));
        }
        else if(size!=2&&size!=0)
        {
            printf("jiance others:%d\n",size);
        }
    }   
    int size=node_size(node1);
    node1=adjust_edges_to_loop_from_nodes(node1,m);
    int size1=node_size(node1);
    printf("size :%d size1:%d\n",size,size1 );
    while(size1!=size)
    {
        node1=adjust_edges_to_loop_from_nodes(node1,m);
        size=size1;
        size1=node_size(node1);
    }
     printf("size :%d size1:%d\n",size,size1 );
/***
**/
//**********
    ////********还要再删除一下孤立点，注意孤立点附着的mesh也要清空
//*******
/***
**/
    for(auto it=m->v_begin(m);it!=m->v_end(m);it++)
    {
        if(node_size(quote(it)->faces)==0)
        {
            Crossover_Point* cp=(Crossover_Point*)(quote(it)->prop);
            for(int i=0;i<2;i++)
            {
                if(cp->vs[i]!=NULL)
                {
                    Node* node=(Node*)(cp->vs[i]->prop);
                    cp->vs[i]->prop=node_delete_value(node,quote(it)); 
                }
                else if(cp->fs[i]!=NULL)
                {
                    Node* node=(Node*)(cp->fs[i]->prop);
                    cp->fs[i]->prop=node_delete_value(node,quote(it));
                }
                else if(cp->cs[i]!=NULL)
                {
                    Node* node=(Node*)(cp->cs[i]->prop);
                    cp->cs[i]->prop=node_delete_value(node,quote(it));
                }

            }
        }

    }


    free_node(node1);
    free_node(node4);

    // for(auto it=m->v_begin(m);it!=m->v_end(m);it++)
    // {
    //     size=node_size(quote(it)->faces);
    //     if(size==1)
    //     {
    //         Crossover_Point* cp=(Crossover_Point*)(quote(it)->prop);
    //         int flag=0;
    //         for(int i=0;i<2;i++)
    //         {
    //             if(cp->vs[i]!=NULL)
    //             {
    //                 if(m->vertex_is_boundary(m,*(cp->vs[i])))
    //                 {
    //                     flag=1;
    //                 }
    //             }
    //             else if(cp->fs[i]!=NULL)
    //             {
    //                 if(m->face_is_boundary(m,*(cp->fs[i])))
    //                 {
    //                     flag=1;
    //                 }
    //             }
    //         } 
    //         if(flag==0)
    //         {
    //             printf("jiance 1 %d %.8lf %.8lf %.8lf\n",quote(it)->id,quote(it)->point[0],quote(it)->point[1],quote(it)->point[2]);
    //             //node1=node_overlying(node1,quote(it));
    //         }
    //     }
    //     else if(size==4)
    //     {
    //         printf("jiance 4:%d\n",quote(it)->id );
    //         //node4=node_overlying(node4,quote(it));
    //     }
    //     else if(size!=2)
    //     {
    //         printf("jiance others %d %d\n",quote(it)->id,size);
    //     }
    // } 

}

static inline void my_vertex_init_(My_Vertex* myv)
{
    myv->v=NULL;
    myv->mesh=NULL;
    myv->id=-1;
    myv->dis=0;
    myv->prop=NULL;
}

static inline double my_distance_two_points(double* p1,double* p2)
{
    double a[3]={p2[0]-p1[0],p2[1]-p1[1],p2[2]-p1[2]};
    double dis=a[0]*a[0]+a[1]*a[1]+a[2]*a[2];

    return SAFE_SQRT(dis);
}
static inline Node* my_get_cell_boundary_vertices(template_c*c,Mesh* m1,Mesh *m)
{
    Node* re=NULL;
    Node* hfs=Mesh_adjust_halffaces(m1,c);
    for(Node* nit=hfs;nit!=NULL;nit=(Node*)(nit->Next))
    {
        template_hf*hf=(template_hf*)(nit->value);
        template_v* v=hf->vertices[0];
     //   printf("vv :%d\n",v->id );
        My_Vertex* mv=(My_Vertex*)malloc(sizeof(My_Vertex));
        my_vertex_init_(mv);
        if(v->prop!=NULL)
        {
            mv->mesh=m;
            mv->v=(template_v*)(((Node*)(v->prop))->value);  
        }
        else{
            mv->mesh=m1;
            mv->v=v;
        }
        re=node_pushback(re,mv);
        template_f* f=(template_f*)(hf->face);
        for(Node*nit=(Node*)(f->prop);nit!=NULL;nit=(Node*)(nit->Next))
        {
            mv=(My_Vertex*)malloc(sizeof(My_Vertex));
            my_vertex_init_(mv);
            mv->mesh=m;
            mv->v=(template_v*)(nit->value);
            mv->dis=my_distance_two_points(mv->v->point,v->point);
            //
            for(Node* nit1=re;nit1!=NULL;nit1=(Node*)(nit1->Prev))
            {
                My_Vertex* mv1=(My_Vertex*)(nit1->value);
                if(mv->dis>=mv1->dis)
                {
                    if(re==nit1)
                    {
                        re=node_pushback(re,mv);
                    } 
                    else
                    {
                        Node*nn=(Node*)malloc(sizeof(Node));
                        Node_init(nn);
                        nn->value=mv;
                        if(nit1->Next!=NULL)
                        {
                            ((Node*)(nit1->Next))->Prev=nn;
                        }
                        nn->Next=nit1->Next;
                        nit1->Next=nn;
                        nn->Prev=nit1;
                        //re=nn;

                    }
                    break;
                }
            }

        }

    }
    free_node(hfs);

    return node_reverse(re);
}
static inline bool my_vertex_is_attach_cell(template_v* v,template_c*c,Mesh* m1)
{
    Crossover_Point* cp=(Crossover_Point*)(v->prop);
    for(int i=0;i<2;i++)
    {
        if(cp->cs[i]==c&&cp->cs[i]!=NULL)
        {
            return true;
        }
        else if(cp->fs[i]!=NULL&&m1->face_is_in_cell(m1,cp->fs[i],c))
        {
            return true;
        }
        else if(cp->vs[i]!=NULL&&m1->vertex_is_in_cell(m1,cp->vs[i],c))
        {
            return true;
        }
    }
    return false;

}
static inline bool my_vertex_is_in_boundary(template_v* v,Node* bn)
{   
    for(Node* nit=bn;nit!=NULL;nit=(Node*)(nit->Next))
    {
        My_Vertex* mv=(My_Vertex*)(nit->value);
        if(mv->v==v)
        {
            return true;
        }
    }

    return false;

}


static inline Node* my_create_split_edges(iterator_edge it,Node* bn,int i,template_c* c,Mesh* m1,Mesh*m)
{
    Node* re=NULL;
    My_Vertex* mv=(My_Vertex*)malloc(sizeof(My_Vertex));
    my_vertex_init_(mv);
    mv->mesh=m;
    mv->v=it.v;
    re=node_overlying(re,mv);
    it++;
    if(it.v!=NULL&&it.v!=((My_Vertex*)(node_at(bn,i+1)->value))->v&&it.v!=((My_Vertex*)(node_at(bn,i-1)->value))->v&&my_vertex_is_attach_cell(it.v,c,m1))
    {
        int flag=1;
        while(flag)
        {
            if(my_vertex_is_in_boundary(it.v,bn))
            {
                flag=0;
            }
            mv=(My_Vertex*)malloc(sizeof(My_Vertex));
            my_vertex_init_(mv);
            mv->v=it.v;
            mv->mesh=m;
            re=node_overlying(re,mv);
        }
        //printf("one edge\n");
        return re; 
    }
    else
    {
        free_node_value(re);
        free_node(re);
        return NULL;
    }
    return NULL; 
}
static inline bool my_edge_is_equal(Node* edge1,Node* edge2)
{
    int size1=node_size(edge1);
    int size2=node_size(edge2);
    if(size1!=size2)
    {
        return false;
    } 
    Node* nit1=edge1,*nit2=edge2;int flag=1;
    for(;nit1!=NULL;nit1=(Node*)(nit1->Next))
    {
        My_Vertex* mv1=(My_Vertex*)(nit1->value);
        My_Vertex* mv2=(My_Vertex*)(nit2->value);
        if(mv1->v!=mv2->v)
        {
            flag=0;
            break;
        }
        nit2=(Node*)(nit2->Next);
    }
    if(flag)
    {
        return true;
    }
    nit1=edge1;nit2=node_reverse(edge2);flag=1;
    for(;nit1!=NULL;nit1=(Node*)(nit1->Next))
    {
        My_Vertex* mv1=(My_Vertex*)(nit1->value);
        My_Vertex* mv2=(My_Vertex*)(nit2->value);
        if(mv1->v!=mv2->v)
        {
            flag=0;
            break;
        }
        nit2=(Node*)(nit2->Prev);
    }
    if(flag)
    {
        return true;
    }
    return false;
}
static inline bool my_edge_is_exist_in_edges(Node* edges,Node* edge)
{
    if(edge==NULL)
    {
        return true;
    }
    for(Node* nit=edges;nit!=NULL;nit=(Node*)(nit->Next))
    {
        Node* node=(Node*)(nit->value);
        if(my_edge_is_equal(node,edge))
        {
            return true;
        }
    }
    return false;
}

static inline Node* my_get_edges_from_boundary(template_c* c,Node* bn,Mesh* m1,Mesh*m)
{
    Node* edges=NULL;
    int i=0;
    for(Node* nit=bn;nit!=NULL;nit=(Node*)(nit->Next))
    {
        My_Vertex* mv=(My_Vertex*)(nit->value);
        if(mv->mesh==m)
        {
            Node* node=mv->v->faces;
            template_f* f1=(template_f*)(node->value);
      
            auto it1=m->edge_begin(m,*(mv->v),*f1);
            Node* edge1=my_create_split_edges(it1,bn,i,c,m1,m);
            if(my_edge_is_exist_in_edges(edges,edge1))
            {
                free_node_value(edge1);
                free_node(edge1);
            }
            else
            {
               // printf("one ege\n");
                edges=node_overlying(edges,edge1);
            }
            node=(Node*)(node->Next);
            if(node!=NULL)
            {   
                template_f* f2=(template_f*)(node->value);

                auto it2=m->edge_begin(m,*(mv->v),*f2);
                Node* edge2=my_create_split_edges(it2,bn,i,c,m1,m);
                if(my_edge_is_exist_in_edges(edges,edge2))
                {
                    free_node_value(edge2);
                    free_node(edge2);
                }
                else
                {
                    //printf("one edge\n");
                    edges=node_overlying(edges,edge2);
                }  
            }
        } 
        i++;
    } 
    return edges;
}

static inline Node* my_split_areas2(Node* bn,Node* edge)
{
    My_Vertex* mv1=(My_Vertex*)(edge->value);
    My_Vertex* mv2=(My_Vertex*)(node_reverse(edge)->value);
    int i=0,index1=-1,index2=-1; 
    for(Node*nit=bn;nit!=NULL;nit=(Node*)(nit->Next))
    {
        My_Vertex* mv=(My_Vertex*)(nit->value);
        if(mv->v==mv1->v)
        {
            index1=i;
        }
        if(mv->v==mv2->v)
        {
            index2=i;
        }
        i++;
    }
    Node* re=NULL;
    if(index1==-1||index2==-1)
    {
    
        return NULL;
    }
    else
    {
        int size=node_size(bn);
        Node* bn1=NULL,*bn2=NULL;
        for(Node* nit=edge;nit!=NULL;nit=(Node*)(nit->Next))
        {
            My_Vertex* mv=(My_Vertex*)(nit->value);
            My_Vertex* mv1=(My_Vertex*)malloc(sizeof(My_Vertex));
            my_vertex_init_(mv1);
            //My_Vertex* mv2=(My_Vertex*)malloc(sizeof(My_Vertex));
            //my_vertex_init_(mv2);
            mv1->v=mv->v;
            //mv2->v=mv->v;
            mv1->mesh=mv->mesh;
            //mv2->mesh=mv->mesh;
            bn1=node_overlying(bn1,mv1);
            //bn2=node_overlying(bn2,mv2);
        }
        i=index2;
        i=(i-1+size)%size;
        while(i!=index1)
        {
            Node* node=node_at(bn,i);
            My_Vertex*mv=(My_Vertex*)(node->value);
            My_Vertex* mv1=(My_Vertex*)malloc(sizeof(My_Vertex));
            my_vertex_init_(mv1);
            mv1->v=mv->v;mv1->mesh=mv->mesh;

            bn1=node_overlying(bn1,mv1);
            i=(i-1+size)%size; 

           // printf("dd %d\n",i);
        } 
        for(Node* nit=node_reverse(edge);nit!=NULL;nit=(Node*)(nit->Prev))
        {
            My_Vertex* mv=(My_Vertex*)(nit->value);
            My_Vertex* mv1=(My_Vertex*)malloc(sizeof(My_Vertex));
            my_vertex_init_(mv1);
            mv1->v=mv->v;
            mv1->mesh=mv->mesh;
            bn2=node_overlying(bn2,mv1);

        }

        i=index1;
        i=(i-1+size)%size;
        while(i!=index2)
        {
            Node* node=node_at(bn,i);
            My_Vertex*mv=(My_Vertex*)(node->value);
            My_Vertex* mv1=(My_Vertex*)malloc(sizeof(My_Vertex));
            my_vertex_init_(mv1);
            mv1->v=mv->v;mv1->mesh=mv->mesh;
            bn2=node_overlying(bn2,mv1);
            i=(i-1+size)%size;  

         //   printf("dd %d\n",i);
        }  
        re=node_overlying(re,bn1);
        re=node_overlying(re,bn2);
    } 
    return re;
}
static Node* my_get_loops(template_c* c,Mesh* m1,Mesh* m)
{
    Node* bn=my_get_cell_boundary_vertices(c,m1,m);
    // printf("bn\n");
    // for(Node* nit=bn;nit!=NULL;nit=(Node*)(nit->Next))
    // {
    //     My_Vertex* mv=(My_Vertex*)(nit->value);
    //     printf("%d ",mv->v->id);
    // }
    // printf("\n");
    Node* bns=NULL;
    bns=node_overlying(bns,bn);

    //int size=node_size(bn);
  //  printf("node size bn:%d \n",size );
    Node* split_edges=my_get_edges_from_boundary(c,bn,m1,m);
      
 //   printf("split_edges:%d bns:%d\n",node_size(split_edges),node_size(bns));
    for(Node* nit=split_edges;nit!=NULL;nit=(Node*)(nit->Next))
    {
        Node* edge=(Node*)(nit->value);
        Node* temp_n=NULL;
        for(Node* nit1=bns;nit1!=NULL;nit1=(Node*)(nit1->Next))
        {
            Node* bn=(Node*)(nit1->value);
            Node* ls=my_split_areas2(bn,edge);
            if(ls==NULL)
            {
                temp_n=node_overlying(temp_n,bn);
            }  
            else
            {
                Node*nr=node_reverse(ls);
                nr->Next=temp_n;
                if(temp_n!=NULL)
                {

                    temp_n->Prev=nr;
                }
                temp_n=ls; 
                free_node_value(bn);
                free_node(bn); 
                break;
            }
        } 
        free_node(bns);
        bns=temp_n;
        free_node_value(edge);
        free_node(edge);
    }

    free_node(split_edges);
    return bns;
}


static Node* my_subidvision1(Node* loop)
{
    Node* re=NULL;
    int len=node_size(loop);
    if(len==3)
    {
        re=node_overlying(re,loop);
        return re;
    }

    double** v=(double**)malloc(sizeof(double*)*len);
    int i=0;
    for(Node* nit=loop;nit!=NULL;nit=(Node*)(nit->Next))
    {
        My_Vertex* mv=(My_Vertex*)(nit->value);
        //v[i]=(double*)malloc(sizeof(double)*3);
        v[i]=mv->v->point;
        i++;
    }
    i=0;
    int **s=(int**)malloc(sizeof(int*)*(len-2));
    for(;i<len-2;i++)
    {
        s[i]=(int*)malloc(sizeof(int)*3);
    }
    subdivision_of_polygon(v,len,s);
    i=0;
    for(;i<len-2;i++)
    {
        Node* node=NULL;
        for(int j=0;j<3;j++)
        {
            Node* nn=node_at(loop,s[i][j]);
            My_Vertex* mv=(My_Vertex*)(nn->value);
            My_Vertex* mv1=(My_Vertex*)malloc(sizeof(My_Vertex));
            my_vertex_init_(mv1);
            mv1->mesh=mv->mesh;
            mv1->v=mv->v;

            node=node_pushback(node,mv1);
        }
        free(s[i]);
        re=node_overlying(re,node_reverse(node));
    } 


    free(v);free(s);
    free_node_value(loop);
    free_node(loop);
    return re;

}
Node* my_create_cell_required_vertices(template_c* c,Mesh* m1,Mesh* m,Mesh* newm)
{
    Node* loops=my_get_loops(c,m1,m);

    // for(Node* nit=loops;nit!=NULL;nit=(Node*)(nit->Next))
    // {
    //     Node* bn=(Node*)(nit->value);
    //     printf("loop\n");
    //     for(Node* nit=bn;nit!=NULL;nit=(Node*)(nit->Next))
    //     {
    //         My_Vertex* mv=(My_Vertex*)(nit->value);
    //         printf("%d ",mv->v->id);
    //     }
    //     printf("\n"); 

    // }

    Node* node_c=NULL;
    for(Node* nit=loops;nit!=NULL;nit=(Node*)(nit->Next))
    {
        Node* loop=(Node*)(nit->value);

        Node* nn=my_subidvision1(loop);

        Node* temp_n=node_reverse(nn);
        temp_n->Next=node_c;
        if(node_c!=NULL)
        {
            node_c->Prev=temp_n;
        }   
        node_c=nn;


    }

    // for(Node* nit=node_c;nit!=NULL;nit=(Node*)(nit->Next))
    // {
    //     Node* nn=(Node*)(nit->value);

    //     printf("triangle :%d  ",node_size(nn) ); 
    //     for(Node* nit1=nn;nit1!=NULL;nit1=(Node*)(nit1->Next))
    //     {
    //         My_Vertex* mv=(My_Vertex*)(nit1->value);
    //         printf("%d ",mv->v->id );

    //     }
    //     printf("\n");

    // }




    free_node(loops);

    //printf("loops size:%d\n",node_size(loops) );

    return node_c;
}
//@m1:: remesh mesh;
//@ m:: edges
//@return remeshed mesh
Mesh* my_intersection_remesh(Mesh* m1,Mesh*m)
{
    Mesh* re=(Mesh*)malloc(sizeof(Mesh));
    Mesh_init(re);
    re->dimension=2;
    re->manifold_require=1;
    re->simplex=1; 
    Int_RB_Tree* tree=(Int_RB_Tree*)malloc(sizeof(Int_RB_Tree));
    int_rb_tree_init(tree);
    //m
    Int_RB_Tree* tree1=(Int_RB_Tree*)malloc(sizeof(Int_RB_Tree));
    int_rb_tree_init(tree1);
    //m1 
    template_v* temp_v[3];
    int num=0;
    for(auto it=m1->c_begin(m1);it!=m1->c_end(m1);it++)
    {
        Node* temp_n=my_create_cell_required_vertices(quote(it),m1,m,re);
        for(Node* nit=temp_n;nit!=NULL;nit=(Node*)(nit->Next))
        {
            Node* nn=(Node*)(nit->value);
            memset(temp_v,0,sizeof(template_v*)*3);
            int i=0;
          // printf("tria ");
            for(Node* nit1=nn;nit1!=NULL;nit1=(Node*)(nit1->Next))
            {
                My_Vertex* mv=(My_Vertex*)(nit1->value);
                if(mv->mesh==m)
                {
                    if(m->get_vertexp(m,mv->v->id)!=mv->v)
                    {
                        printf("cuowu\n");
                    }
                    temp_v[i]=(template_v*)(tree->find(tree,mv->v->id));
                    if(temp_v[i]==NULL)
                    {
                       temp_v[i]=re->create_vertexv(re,mv->v->point,3);
                        tree->insert(tree,mv->v->id,temp_v[i]);
                    }
                    //printf("here\n");

                } 
                else if(mv->mesh==m1)
                {
                    if(m1->get_vertexp(m1,mv->v->id)!=mv->v)
                    {
                        printf("cuowu\n");
                    } 
                    temp_v[i]=(template_v*)(tree1->find(tree1,mv->v->id));
                    if(temp_v[i]==NULL)
                    {
                       temp_v[i]=re->create_vertexv(re,mv->v->point,3);
                        tree1->insert(tree1,mv->v->id,temp_v[i]);
                    }
                  //  printf("her1\n");
                }
                else
                {
                    printf("cuowu\n");
                }
              // printf("%d ",mv->v->id );

                i++;
            }
           // printf("\n");

            re->create_cellv(re,temp_v,3);
            free_node_value(nn);
            free_node(nn);
        }
        if(node_size(temp_n)>1)
        {
            num++;
        }
        // if(num>4)
        // {
        //     break;
        // }


        free_node(temp_n);
      
    }
    printf("mesh numc:%d\n",re->num_c(re) );
    re->prop=tree;
    int_rb_tree_free(tree1); 




   return re; 
}





static Int_RB_Tree* get_one_edges_from_intersection(Int_RB_Tree* tree,Mesh* m)
{

    Int_RB_Tree* re=(Int_RB_Tree*)malloc(sizeof(Int_RB_Tree));
    int_rb_tree_init(re);
    auto it=tree->begin(tree);
    template_f* f=(template_f*)(*it);
    if(f==NULL)
    {
        return re;
    }

    Node* tempn=NULL;
    tempn=node_overlying(tempn,f);
    tree->erase(tree,f->id);
    re->insert(re,f->id,f);
    while(tempn!=NULL)
    {
        Node* tempn1=NULL;
        for(Node* nit=tempn;nit!=NULL;nit=(Node*)(nit->Next))
        {
            template_f*f=(template_f*)(nit->value);
            for(int i=0;i<f->vertices_size;i++)
            {
                for(auto vf=m->vf_begin(m,*(f->vertices[i]));vf!=m->vf_end(m,*(f->vertices[i]));vf++)
                {   
                    template_f* f1=quote(vf);
                    if(tree->find(tree,f1->id)==NULL)
                    {
                        continue;
                    }
                    tempn1=node_overlying(tempn1,f1);
                    tree->erase(tree,f1->id);
                    re->insert(re,f1->id,f1);
                }
            }
        }
        free_node(tempn);
        tempn=tempn1;
    } 
    return re;

}

static void my_mesh_cut_along_the_curvef(Mesh* m1,Int_RB_Tree* tree)
{

    Int_RB_Tree* tr=(Int_RB_Tree*)(m1->prop);

    template_v* temp_v[2]={NULL,NULL};
    Node* nfs=NULL;
    for(auto it=tree->begin(tree);it.it!=NULL;it++)
    {
        template_f* f=(template_f*)(*it); 
        temp_v[0]=(template_v*)(tr->find(tr,f->vertices[0]->id));
        temp_v[1]=(template_v*)(tr->find(tr,f->vertices[1]->id));
        template_f* f1=m1->get_facev(m1,temp_v,2); 
        nfs=node_overlying(nfs,f1);
        if(f1==NULL)
        {
            printf("cuowu here %d %d\n",temp_v[0]->id,temp_v[1]->id);
        } 
 

    }

    Mesh_cut_along_the_curvef(m1,nfs);
    free_node(nfs);
}
void my_intersection_cut(Mesh* m1,Mesh* m)
{

    Int_RB_Tree* tree=int_rb_tree_copy(m->faces);
 
    while(tree->size>0)
    {
        Int_RB_Tree* tree1=get_one_edges_from_intersection(tree,m);
        my_mesh_cut_along_the_curvef(m1,tree1); 
        int_rb_tree_free(tree1);

    }


    int_rb_tree_free(tree);
//    printf("here end\n");
  
}
